package cn.itcast.bos.service.courier.imp;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.CourierRepository;
import cn.itcast.bos.dao.FixedAreaRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.courier.CourierService;
import cn.itcast.crm.domain.Customer;

@Service
@Transactional
public class CourierServiceImp implements CourierService {
	@Autowired
	private CourierRepository courierRepository;

	public void save(Courier courier) {
		courierRepository.save(courier);

	}

	public Page<Courier> findPageData(Pageable pageable) {

		return courierRepository.findAll(pageable);
	}


	public Page<Courier> findPageData(Specification<Courier> specification,
			Pageable pageable) {
		
		return courierRepository.findAll(specification, pageable);
	}

	@Override
	public void delete(String ids) {
		 String [] arr=ids.split("-");
		 for(int i = 0; i < arr.length; i++){
			 int id= Integer.parseInt(arr[i]);
		Courier c= courierRepository.findOne(id);
		c.setDeltag('1');
			 
		 }
		
	}

	@Override
	public void restore(String ids) {
		 String [] arr=ids.split("-");
		 for(int i = 0; i < arr.length; i++){
			 int id= Integer.parseInt(arr[i]);
		Courier c= courierRepository.findOne(id);
		c.setDeltag('0');
		
	}

	}
	
	@Autowired
	private FixedAreaRepository fixedAreaRepository;


	public List<Courier> findNoLinkedCourier(String fixedAreaId) {
		
		final FixedArea fixedArea = fixedAreaRepository.findOne(fixedAreaId);
		//查找与这个定区没有关联的 客户
		// 条件查询
		Specification<Courier> spec=new Specification<Courier>() {
			
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				Predicate p=cb.isNotMember(fixedArea, root.get("fixedAreas").as(Set.class)  );
				//Predicate p=cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return p;
			}
		};
		
		
		return courierRepository.findAll(spec);
	}

	@Override
	public List<Courier> findLinkCourier(String fixedid) {
		final FixedArea fixedArea=fixedAreaRepository.findOne(fixedid);
		Specification<Courier> spec= new Specification<Courier>() {

			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				Predicate p=cb.isMember(fixedArea, root.get("fixedAreas").as(Set.class));
				return p;
			}
		};
		
		
		return courierRepository.findAll(spec);
	}
}
